<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>疾病管理</title>
    <title>vue-demo1</title>
    <script src="/js/vue.js"></script>
    <link rel="stylesheet" href="/css/index.css">
    <script src="/js/index.js"></script>
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <!--查询-->
    <el-row>
        <el-col :span="4"><el-input  v-model="searchForm.diseaseN"  size="small" placeholder="请输入疾病名称"></el-input></el-col>
        <el-col :span="4"><el-input v-model="searchForm.departmentI"  size="small" placeholder="请输入科室id"></el-input></el-col>
        <el-col :span="4"><el-button type="primary" @click="search" size="small" icon="el-icon-search">搜索</el-button></el-col>
        <el-col :span="4"><el-button type="primary" @click="handlerAdd"  size="small" icon="el-icon-circle-plus">添加</el-button></el-col>
        <el-col :span="4"></el-col>
        <el-col :span="4"></el-col>
    </el-row>
    <!--表格显示-->
    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column
                prop="diseaseId"
                label="Id">
        </el-table-column>
        <el-table-column
                prop="diseaseName"
                label="疾病名称">
        </el-table-column>
        <el-table-column
                prop="symptoms"
                label="症状">
        </el-table-column>
        <el-table-column
                prop="treatment"
                label="治疗方针">
        </el-table-column>
        <el-table-column
                prop="department.departmentName"
                label="科室名称">
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[2,5,10,15,20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!--更新对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="疾病名称" :label-width="formLabelWidth">
                <el-input v-model="form.diseaseName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="症状" :label-width="formLabelWidth">
                <el-input v-model="form.symptoms" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="治疗方针" :label-width="formLabelWidth">
                <el-input v-model="form.treatment" autocomplete="off"></el-input>
            </el-form-item>
          <!--  <el-form-item label="科室id" :label-width="formLabelWidth">
                <el-select v-model="form.departmentId" placeholder="请选择科室">
                    <el-option
                            v-for="item in options"
                            :key="item.departmentId"
                            :label="item.departmentId"
                            :value="item.departmentId">
                    </el-option>
                </el-select>
            </el-form-item>-->

            <!-- 科室名称 -->
            <el-form-item label="科室名称" :label-width="formLabelWidth">
                <el-select v-model="form.departmentId" placeholder="请选择科室名称">
                    <el-option v-for="option in deptOptions"
                               :key="option.departmentId"
                               :label="option.departmentName"
                               :value="option.departmentId"></el-option>
                </el-select>
            </el-form-item>


        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>
    //Vue.component();  //组件->模板


    var app = new Vue({
            el:"#app",
            //存放所有js对象的  plain  javascript object
            data(){
                return{
                    departmentId:[],
                    // 存储科室名称列表
                    deptOptions: [], // 科室选项列表
                    // departmentIds: [], // 存储后台传递的科室ID
                    tableData: [], //表格数据
                    currentPage: 1,//当前页 第几页
                    pageSize: 5,//每页显示数量
                    total: 400,//总条数
                    searchForm:{diseaseN:'',departmentI:''},//搜索对象
                    title: '添加', //弹出框标题
                    dialogFormVisible: false,//对话框是否显示
                    form: { //form表单对象
                        diseaseName: '',
                        symptoms: '',
                        treatment: '',
                        departmentId: '',
                    },
                    formLabelWidth: '120px' //form中label的宽度
                }
            },
        mounted() {
            this.getDepartmentList(); // 获取科室列表
        },
            //所有页面元素触发的事件的方法
            methods:{
                // 获取科室选项列表数据
                getDepartmentList() {
                    axios.get('/disease/departments')
                        .then(response => {
                            this.deptOptions = response.data;
                            this.loadData()
                        })
                        .catch(error => {
                            console.error(error);
                        });
                },
                //封装的加载数据方法
                loadData(){
                    //定义一个变量，赋值为当前vue对象
                    var _this = this;
                    //使用axios向后台发请求，获取数据
                    //axios.post('http://172.16.7.66:8080/dept/queryByPage', {
                    axios.post(//请求方式，必须和后端资源暴露方式一致
                        '/disease/queryByParam',//请求的url
                        {"pageNo":this.currentPage,"pageSize":this.pageSize,
                             "data":{"inspectName":this.searchForm.inspectN,"fee":this.searchForm.feE}}) //请求携带的参数


                        .then(function (response) {//成功响应的处理方法   response 是响应的数据
                            console.log(response);
                            //response 一定是json对象  stringify 把json对象转json字符串
                            //alert(JSON.stringify(response));
                            //判断返回是否正确
                            if(response.data.code==200){
                                //直接使用this 代表axios的对象  this
                                _this.tableData=response.data.data.list;
                                _this.total=response.data.data.total;
                            }else{
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                },
                //处理每页数量发生变化
                handleSizeChange(val) {
                    console.log(`每页 ${val} 条`);
                    //让每页数量发生改变
                    this.pageSize=val;
                    //调用加载数据方法
                    this.loadData();
                },
                //处理当前页（页码 第几页）发生变化
                handleCurrentChange(val) {
                    console.log(`当前页: ${val}`);
                    //让第几页发生改变
                    this.currentPage=val;
                    //调用加载数据方法
                    this.loadData();
                },
                //按条件查询
                search(){
                    //直接调用loadData
                    this.loadData();
                },
                //处理添加
                handlerAdd(){
                    //修改标题
                    this.title='添加';
                    //清空原来的值
                    this.form={};
                    //让对话框显示
                    this.dialogFormVisible = true;
                },
                //处理更新
                handleEdit(index, row) {
                    console.log(index, row);
                    this.title = '更新';
                    this.form = row;
                    //alert(JSON.stringify(this.form));
                    //让对话框显示
                    this.dialogFormVisible = true;
                },
//调用接口添加或者更新数据
                addOrUpdate() {
                    //定义url
                    var url = "/disease/add";
                    //判断是否添加还是更新
                    if (this.title === '更新') {
                        url = "/disease/update";
                    }
                    //定义一个变量，赋值为当前vue对象
                    var _this = this;
                    //使用axios向后台发请求，获取数据
                    //axios.post('http://172.16.7.66:8080/dept/queryByPage', {
                    axios.put(//请求方式，必须和后端资源暴露方式一致
                        url,//请求的url
                        this.form)
                        .then(function (response) {//成功响应的处理方法   response 是响应的数据
                            console.log(response);
                            //response 一定是json对象  stringify 把json对象转json字符串
                            //alert(JSON.stringify(response));
                            //判断返回是否正确
                            if (response.data.code == 200) {
                                //提示信息
                                _this.$message({
                                    message: '恭喜你，这是一条成功消息',
                                    type: 'success'
                                });

                                //关闭对话框
                                _this.dialogFormVisible = false;
                                //重新加载数据
                                _this.loadData();
                            } else {
                                _this.$message.error('错了哦，这是一条错误消息');
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                },
                //处理删除
                handleDelete(index, row) {
                    this.$confirm('此操作将永久删除该数据，是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.delete('/disease/deleteById', {params: {diseaseId: row.diseaseId}}).then(response => {
                            if (response.data.code == 200) {
                                this.loadData();
                                this.$message.success('删除成功了')
                            } else {
                                this.$message.success('删除失败了')
                            }
                        }).catch(function (error) {
                            console.log(error);
                            this.$message.success('删除失败了')
                        });
                    })}
            },
          /*  beforeCreate(){
               alert("创建前----");
            },*/
            created(){//初始化操作
                //alert("创建后----");
                this.loadData();
            },
        }
    )
</script>
</body>
</html>
